import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        component: () =>
            import ('@/views/login')
    },
    {
        path: '/home',
        redirect: '/welcome',
        meta: {
            auth: true
        },
        component: () =>
            import ('@/views/home'),
        children: [{
                path: '/welcome',
                component: () =>
                    import ('@/views/welcome')
            },
            {
                path: '/orders',
                component: () =>
                    import ('@/views/orders')
            },
            {
                path: '/params',
                component: () =>
                    import ('@/views/params')
            },
            {
                path: '/reports',
                component: () =>
                    import ('@/views/reports')
            },
            {
                path: '/rights',
                component: () =>
                    import ('@/views/rights')
            },
            {
                path: '/roles',
                component: () =>
                    import ('@/views/roles')
            },
            {
                path: '/users',
                component: () =>
                    import ('@/views/users')
            },
            {
                path: '/addgood',
                component: () =>
                    import ('@/views/goods/addgood')
            },
            {
                path: '/goods',
                component: () =>
                    import ('@/views/goods'),
            },
            {
                path: '/categories',
                component: () =>
                    import ('@/views/categories')
            },

        ]
    }
]

const router = new VueRouter({
    routes
})
router.beforeEach((to, from, next) => {
    console.log('路由', to.matched)
    const arr = to.matched.some(item => {
        return item.meta.auth
    })
    if (arr) {
        const flag = sessionStorage.getItem('token')
        if (!flag) {
            next({
                path: '/login'
            })
        } else {
            next()
        }
    } else {
        next()
    }

})
export default router